//
//  GCMOUTLINEEquPlanViewController.m
//  GeeCityManager
//
//  Created by 聚城科技 on 16/12/28.
//  Copyright © 2016年 聚城科技. All rights reserved.
//

#import "GCMOUTLINEEquPlanViewController.h"
#import "GCMScanViewController.h"
#import "GCMUploadViewController.h"
#import "GCMDownLoadViewController.h"
#import "GCMEQUShouYeTableViewCell.h"
#import "GCMEQUShouYeModel.h"
#import "GCMEQUErJiViewController.h"
#import "SubLBXScanViewController.h"
#import "GCMSheBeiRequst.h"
#import "GCMSearchViewController.h"
#import "GCMOUTLINETableViewCell.h"
#import "GCMOUTLINESheBei.h"
#import "DBManage.h"
#import "GCMOUTLINECaoZuoViewController.h"


@interface GCMOUTLINEEquPlanViewController ()<UITableViewDelegate,UITableViewDataSource>

@end

@implementation GCMOUTLINEEquPlanViewController{
    
    UIView *_topview;
    UITableView *_tableV;
    NSMutableArray *_dataSource;
    NSMutableArray *_seachArr;
    UIImageView *navBarHairlineImageView;
    UIButton* rigbutton;
    NSInteger _currentPage;
    UILabel *_nilLabel;
}


-(void)viewWillAppear:(BOOL)animated{
    
    //去除导航栏下方的横线
    navBarHairlineImageView.hidden = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    _currentPage = 1;
    [self navigationBar];
    [self downLoadData];
    [self createUI];
    [self createTableView];
    
}
-(void)navigationBar{
    
    navBarHairlineImageView = [self findHairlineImageViewUnder:self.navigationController.navigationBar];
    self.navigationController.navigationBarHidden = NO;
    
}

-(void)downLoadData{
    //1、清空数据源
    [_dataSource removeAllObjects];
    //2、从数据库中读取信息
    [self readData];
    //3、刷新
    [_tableV reloadData];
}
-(void)readData{
    _dataSource = [[NSMutableArray alloc]init];
    DBManage * manage = [DBManage defaultDBMessage];
    NSLog(@"shuju%@",[manage allSheBei]);
    [_dataSource addObjectsFromArray:[manage allSheBei]];
    NSLog(@"shuju%@",_dataSource);
    
}
-(void)createUI{
    self.view.backgroundColor = [UIColor whiteColor];
    
    //    添加头部viewview
    _topview= [[UIView alloc]initWithFrame:CGRectMake(0,64, SCREEN_WIDTH, SCREEN_HEIGHT/5)];
    _topview.backgroundColor  = [UIColor colorWithRed:0.25 green:0.44 blue:0.63 alpha:1];
    //        _topview.backgroundColor  = [UIColor redColor];
    [self.view addSubview:_topview];
    UIImageView *imageV = [[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/8, 0, _topview.bounds.size.height/9*4, _topview.bounds.size.height/9*4)];
    imageV.backgroundColor = [UIColor blueColor];
    imageV.image = [UIImage imageNamed:@"xic_launcher"];
    imageV.layer.cornerRadius =_topview.bounds.size.height/18*4;
    imageV.layer.masksToBounds = YES;
    [_topview addSubview:imageV];
    
    UILabel * titleLab = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/8 + _topview.bounds.size.height/2 , _topview.bounds.size.height/8, SCREEN_WIDTH -(SCREEN_WIDTH/8 + _topview.bounds.size.height/2), _topview.bounds.size.height/4)];
    //    titleLab.backgroundColor = [UIColor grayColor];
    titleLab.text =@"青岛聚城网络科技有限公司";
    titleLab.textColor = [UIColor whiteColor];
    [_topview addSubview:titleLab];
    
    NSArray *nameArr = @[@"扫一扫"];
    NSArray *imageArr = @[@"device_ic_scan"];
    UIButton *button  = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2 - _topview.bounds.size.height/4 , _topview.bounds.size.height/2, _topview.bounds.size.height/2, _topview.bounds.size.height/2)];
    //    button.backgroundColor = [ UIColor colorWithRed:0.50 green:0.55 blue:0.55 alpha:1.0];
    UIImageView *sysIamgeV = [[UIImageView alloc]initWithFrame:CGRectMake(button.bounds.size.width / 2 - button.bounds.size.height/4, 0, button.bounds.size.height/2, button.bounds.size.height/2)];
    //        sysIamgeV.backgroundColor = [UIColor greenColor];
    [sysIamgeV setImage:[UIImage imageNamed:imageArr[0]]];
    [button addSubview:sysIamgeV];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, button.bounds.size.height/2, button.bounds.size.width, button.bounds.size.height/2)];
    label.text = nameArr[0];
    //        label.backgroundColor = [UIColor orangeColor];
    label.textColor = [UIColor whiteColor];
    label.font = [UIFont systemFontOfSize:14];
    label.textAlignment = NSTextAlignmentCenter;
    [button addSubview:label];
    [button addTarget:self action:@selector(onButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [_topview addSubview:button];
    
    
}

-(void)createTableView{
    
    _tableV = [[UITableView alloc]initWithFrame:CGRectMake(0, 64 + SCREEN_HEIGHT/5, SCREEN_WIDTH, SCREEN_HEIGHT  - (64 + SCREEN_HEIGHT/5))];
    _tableV.backgroundColor  = [UIColor redColor];
    _tableV.delegate = self;
    _tableV.dataSource =self;
    _tableV.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1.0];
    [self.view addSubview:_tableV];
    [_tableV setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    GCMOUTLINETableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[GCMOUTLINETableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    [cell setBackgroundColor:[UIColor colorWithRed:.93 green:.93 blue:.93 alpha:1]];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    //    cell.textLabel.textColor = [UIColor colorWithRed:0.39 green:0.39 blue:0.39 alpha:1];
    GCMOUTLINESheBei *listModel = _dataSource[indexPath.row];
    //NSLog(@"%@",listModel.contact);
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell setSheBeiModel:listModel];
    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataSource.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 180;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    GCMOUTLINESheBei *listModel = _dataSource[indexPath.row];
    GCMOUTLINECaoZuoViewController * reciveVC = [[GCMOUTLINECaoZuoViewController alloc]init];
    reciveVC.sbid = listModel.sbid;
    [self.navigationController pushViewController:reciveVC animated:NO ];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
    backItem.title = @"设备计划";
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.navigationItem.backBarButtonItem = backItem;
    
}


-(void)onButtonClick:(UIButton *)button{
    
    SubLBXScanViewController *vc = [SubLBXScanViewController new];
    [self.navigationController pushViewController:vc animated:YES];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
    backItem.title = @"返回";
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.navigationItem.backBarButtonItem = backItem;

}

- (UIImageView *)findHairlineImageViewUnder:(UIView *)view {
    if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
        return (UIImageView *)view;
    }
    for (UIView *subview in view.subviews) {
        UIImageView *imageView = [self findHairlineImageViewUnder:subview];
        if (imageView) {
            return imageView;
        }
    }
    return nil;
}

//在页面消失的时候就让出现
- (void)viewWillDisappear:(BOOL)animated
{
    navBarHairlineImageView.hidden = NO;
    rigbutton.hidden = YES;
    
}
- (void)didReceiveMemoryWarning {
    
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
